package functional;

import org.junit.Test;

/**
 * Author:ZouHeng
 * Description:
 * Date:2021-07-02  17:22
 **/
public class TestFunctional {

    @Test
    public void testSupplier() {
        String msgA = "Hello , ";
        String msgB = "Kobe！";
        SupplierDemo demo = new SupplierDemo();
        System.out.println(demo.getString(() -> msgA + msgB));


        int arr[] = {2, 3, 4, 52, 333, 23};
        int maxNum = demo.getMax(() -> {
            int max = arr[0];
            for (int i = 0; i < arr.length; i++) {
                max = arr[i] > max ? arr[i] : max;
            }
            return max;
        });
        System.out.println(maxNum);
    }

    @Test
    public void testConsumer() {
        ConsumerDemo consumerDemo = new ConsumerDemo();
        consumerDemo.consumerString(str -> System.out.println(str));
    }

    @Test
    public void testConsumerAndThen() {
        ConsumerAndThen andThen = new ConsumerAndThen();
        andThen.consumerString(s -> System.out.println(s.toUpperCase()), s -> System.out.println(s.toLowerCase()));
    }

    /**
     * 下面的字符串数组当中存有多条信息，请按照格式“ 姓名：XX。性别：XX。 ”的格式将信息打印出来。
     */
    @Test
    public void testConsumerPrintInfo() {
        String[] array = {"迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男"};
        ConsumerDemo consumerDemo = new ConsumerDemo();
        consumerDemo.printInfo(s -> System.out.print("姓名：" + s.split(",")[0]),
                               s -> System.out.println(",性别：" + s.split(",")[1]),
                               array);
    }

    @Test
    public void testPredicate() {
        String msg = "HelloWorld";
        PredicateDemo predicateDemo = new PredicateDemo(msg);
        predicateDemo.judgeMsg(s -> s.length() > 5);
    }

    @Test
    public void testPredicateThreeRelation() {
        String msg = "HelloWorld";
        PredicateDemo predicateDemo = new PredicateDemo(msg);
        System.out.println("字符传长度位：" + msg.length());
        System.out.print("字符串长度大于5且小于20，");
        predicateDemo.judgeMsgAnd(s -> s.length() > 5, s -> s.length() < 20);
        System.out.println();
        System.out.print("字符串长度大于5且小于8，");
        predicateDemo.judgeMsgAnd(s -> s.length() > 5, s -> s.length() < 8);
        System.out.println();
        System.out.print("字符串长度大于5或小于8，");
        predicateDemo.judgeMsgOr(s -> s.length() > 5, s -> s.length() < 8);
        System.out.println();
        System.out.print("字符串长度大于5取反，");
        predicateDemo.judgeMsgnegate(s -> s.length() > 5);
    }

    /**
     * 数组当中有多条“姓名+性别”的信息如下，请通过 Predicate 接口的拼装将符合要求的字符串筛选到集合ArrayList 中，需要同时满足两个条件：
     * 1. 必须为女生；
     * 2. 姓名为4个字。
     */
    @Test
    public void testPredicateFilter() {
        String[] array = {"迪丽热巴,女", "古力娜扎,女", "马尔扎哈,男", "赵丽颖,女"};
        PredicateDemo predicateDemo = new PredicateDemo();
        predicateDemo.filter(s -> s.split(",")[0].length() == 4, s -> s.split(",")[1].equals("女"), array);
    }

    @Test
    public void testFunction() {
        String num = "10";
        FunctionDemo functionDemo = new FunctionDemo(num);
        functionDemo.change(s -> Integer.parseInt(s));
    }

    @Test
    public void testFunctionAndThen() {
        String num = "10";
        FunctionDemo functionDemo = new FunctionDemo(num);
        functionDemo.methodAndThen(s -> Integer.parseInt(s) + 10, s -> s + 10);
    }
}
